﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

using FrbaBus.Core;

namespace FrbaBus.Abm_Recorrido
{
    public partial class BusquedaCiudad : Form
    {
        public BusquedaCiudad()
        {
            InitializeComponent();
        }


        private RecorridoCreacion1 parent1 = null;
        private RecorridoModificacion1 parent2 = null;
        private RecorridoModificacion2 parent3 = null;
        private FrbaBus.Compra_de_Pasajes.CompraPasajes parent4 = null;

        public BusquedaCiudad(Form callingForm)
        {
            if (string.Compare(callingForm.Name, "CompraPasajes") == 0)
                parent4 = callingForm as FrbaBus.Compra_de_Pasajes.CompraPasajes;

            if (string.Compare(callingForm.Name, "RecorridoModificacion1") == 0)
                parent2 = callingForm as RecorridoModificacion1;
            else if (string.Compare(callingForm.Name, "RecorridoModificacion2") == 0)
                parent3 = callingForm as RecorridoModificacion2;
            else
                parent1 = callingForm as RecorridoCreacion1;
            InitializeComponent();
        }

            

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            dataGridView1.Visible = true;
            string qrytext ="";
            if (BusqCiudad.Text == "")
                qrytext = "";
            else
                qrytext = " WHERE nombre like '%" + BusqCiudad.Text + "%'";

            DBManager db = new DBManager();
            SqlCommand consulta = new SqlCommand("select * from ALL_IN.ciudadesHabilitadas" + qrytext);
            SQLResponse r = db.executeQuery(consulta);
            SqlCommand consulta2 = new SqlCommand("select count (*) from ALL_IN.ciudadesHabilitadas" + qrytext);
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            for (int i = 0; i < valor; i++)
            {
                dataGridView1.Rows.Add(r.result.Rows[i][0].ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                DataGridViewCell cell = dataGridView1.SelectedCells[0];
                if (parent2 != null)
                {
                    if (this.parent2.origen == true)
                        this.parent2.textBox3.Text = cell.Value.ToString();
                    else
                        this.parent2.textBox4.Text = cell.Value.ToString();
                }
                else if (parent1 != null)
                {
                    if (this.parent1.origen == true)
                    {
                        this.parent1.textBox3.Text = cell.Value.ToString();
                    }
                    else
                        this.parent1.textBox4.Text = cell.Value.ToString();
                }
                else if (parent3 != null)
                {
                    if (this.parent3.origen == true)
                    {
                        this.parent3.textBox3.Text = cell.Value.ToString();
                    }
                    else
                        this.parent3.textBox4.Text = cell.Value.ToString();
                }
                else if (parent4 != null)
                {
                    if (this.parent4.origen == true)
                        this.parent4.textBox3.Text = cell.Value.ToString();
                    else
                        this.parent4.textBox4.Text = cell.Value.ToString();
                }
                this.Close();
            }
        }

        private void BusquedaCiudad_Load(object sender, EventArgs e)
        {

        }



    }
}
